package com.um.jdy.platform.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.commons.service.MobileService;
import com.um.jdy.common.member.enums.CodeKey;
import com.um.jdy.common.platform.code.PlatformCode;
import com.um.jdy.common.platform.entity.po.App;
import com.um.jdy.common.platform.entity.po.CompanyUser;
import com.um.jdy.common.platform.entity.vo.AppVO;
import com.um.jdy.common.platform.service.AppService;
import com.um.jdy.common.platform.service.CompanyUserService;
import com.um.jdy.platform.platform.entity.dto.*;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AppManager extends BaseManager<AppService> {

    @Autowired
    MobileService mobileService;

    @Autowired
    CompanyUserService companyUserService;

    /**
     * 详情
     *
     * @return
     */
    public Response<AppVO> get() {
        App app = service.getEntity("com_id", getComId());
        if(app == null){
            app = new App();
            app.setComId(getComId());
            service.save(app);
        }

        AppVO appVO = UMBeanUtils.copyBean(app,AppVO.class);
        appVO.setAlipayAppSecrect(repaceAll(appVO.getAlipayAppSecrect()));
        appVO.setMiniAppSecrect(repaceAll(appVO.getMiniAppSecrect()));
        appVO.setMpAppSecrect(repaceAll(appVO.getMpAppSecrect()));
        appVO.setWxApiV3Key(repaceAll(appVO.getWxApiV3Key()));
        appVO.setWxCertContent(repaceAll(appVO.getWxCertContent()));
        appVO.setWxKeyContent(repaceAll(appVO.getWxKeyContent()));
        appVO.setSmsAccessKeyId(repaceAll(appVO.getSmsAccessKeyId()));
        appVO.setSmsAccessKeySecret(repaceAll(appVO.getSmsAccessKeySecret()));
        return Response.success(appVO);
    }

    /**
     * 更新微信小程序
     *
     * @param id
     * @param appMiniDTO
     * @return
     */
    public Response updateMini(String id, AppMiniDTO appMiniDTO) {
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        if(appMiniDTO.getMiniAppId().contains("*") || appMiniDTO.getMiniAppSecrect().contains("*") ||
                appMiniDTO.getWxApiV3Key().contains("*") || appMiniDTO.getWxMchId().contains("*") ||
                appMiniDTO.getWxCertContent().contains("*") || appMiniDTO.getWxKeyContent().contains("*") ){
            throw new ServiceException(PlatformCode.IncludeInvalidCode);
        }

        mobileService.checkCode(CodeKey.EditSetting, master.getMobile(), appMiniDTO.getCode());

        App app = BeanUtil.copyProperties(appMiniDTO, App.class);
        app.setPlatformAppId(id);
        service.updateEntity(app);

        mobileService.expireCode(CodeKey.EditSetting, master.getMobile());

        return Response.success();
    }

    /**
     * 更新微信公众号
     *
     * @param id
     * @param appMpDTO
     * @return
     */
    public Response updateMp(String id, AppMpDTO appMpDTO) {
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        if(appMpDTO.getMpAppId().contains("*") || appMpDTO.getMpAppSecrect().contains("*")){
            throw new ServiceException(PlatformCode.IncludeInvalidCode);
        }

        mobileService.checkCode(CodeKey.EditSetting, master.getMobile(), appMpDTO.getCode());

        App app = BeanUtil.copyProperties(appMpDTO, App.class);
        app.setPlatformAppId(id);
        service.updateEntity(app);

        mobileService.expireCode(CodeKey.EditSetting, master.getMobile());
        return Response.success();
    }

    /**
     * 更新支付宝
     *
     * @param id
     * @param appAlipayDTO
     * @return
     */
    public Response updateAlipay(String id, AppAlipayDTO appAlipayDTO) {
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        if(appAlipayDTO.getAlipayAppId().contains("*") || appAlipayDTO.getAlipayAppSecrect().contains("*")){
            throw new ServiceException(PlatformCode.IncludeInvalidCode);
        }

        mobileService.checkCode(CodeKey.EditSetting, master.getMobile(), appAlipayDTO.getCode());

        App app = BeanUtil.copyProperties(appAlipayDTO, App.class);
        app.setPlatformAppId(id);
        service.updateEntity(app);

        mobileService.expireCode(CodeKey.EditSetting, master.getMobile());

        return Response.success();
    }

    /**
     * 更新短信配置
     * @param id
     * @param smsDTO
     * @return
     */
    public Response updateSms(String id, SmsDTO smsDTO){
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        if(StringUtil.equals(YesOrNo.Yes.name(),smsDTO.getSmsIsEnable())) {
            if (smsDTO.getSmsAccessKeyId().contains("*") || smsDTO.getSmsAccessKeySecret().contains("*")) {
                throw new ServiceException(PlatformCode.IncludeInvalidCode);
            }
        }else{
            SmsDTO dto = new SmsDTO();
            dto.setSmsIsEnable(smsDTO.getSmsIsEnable());
            smsDTO = dto;
        }


        App app = BeanUtil.copyProperties(smsDTO, App.class);
        app.setPlatformAppId(id);
        service.updateEntity(app);

        return Response.success();
    }

    /**
     * 更新商城入口配置
     * @param id
     * @param mallDTO
     * @return
     */
    public Response updateMall(String id, MallDTO mallDTO){
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }


        App app = BeanUtil.copyProperties(mallDTO, App.class);
        app.setPlatformAppId(id);
        service.updateEntity(app);

        return Response.success();
    }

    /**
     * 发送验证码到平台管理员手机
     * @return
     */
    public Response sendMobileCode(){
        CompanyUser master = companyUserService.getEntity(new String[]{"com_id","is_master"},new Object[]{getComId(),YesOrNo.Yes.name()});
        if(master == null){
            throw new ServiceException(PlatformCode.PlatformNotExist);
        }

        mobileService.sendCode(CodeKey.EditSetting, master.getMobile(),getComId());

        return Response.success(master.getMobile());
    }


    /**
     * 替换字符
     * @param value
     * @return
     */
    private String repaceAll(String value){
        if(value == null || value.length() == 0){
            return value;
        }

        String start = "";
        if(value.length() > 4)
            start = value.substring(0,4);
        else
            start = value.substring(0);

        String end = "";
        if(value.length() > 4)
            end = value.substring(value.length() - 4);

        StringBuilder sb = new StringBuilder();
        sb.append(start);

        for(int index = 0;index < value.length() - 8;index ++){
            sb.append("*");
        }

        sb.append(end);
        return sb.toString();
    }
}
